/*
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
package logmatcher.buffers;

import java.util.ArrayList;
import java.util.List;

import logmatcher.InputFilter;
import logmatcher.LogEventListener;
import logmatcher.LogEventProducer;

import org.apache.commons.collections.Buffer;
import org.apache.commons.collections.buffer.UnboundedFifoBuffer;
import org.apache.commons.lang.NotImplementedException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


/**
 *  Implementation of LogBuffer. Enables using message listeners without filtering mechanism. Simple and fast
 *  implementation, advised if no filters at all are used.
 *
 *  @version $Rev: 471 $
 */
public class SimpleBuffer implements LogEventListener, LogEventProducer {
    //~ Static fields/initializers -------------------------------------------------------------------------------------

    /** Class logger. */
    private static final Log LOGGER = LogFactory.getLog(SimpleBuffer.class);

    //~ Instance fields ------------------------------------------------------------------------------------------------

    /** Buffer for messages. */
    private Buffer messageBuffer;

    /** List of message listeners. */
    private List<LogEventListener> listeners;

    //~ Constructors ---------------------------------------------------------------------------------------------------

/**
     *  Creates a new SimpleBuffer object.
    */
    public SimpleBuffer() {
        messageBuffer = new UnboundedFifoBuffer();
        listeners = new ArrayList<LogEventListener>();
    }

    //~ Methods --------------------------------------------------------------------------------------------------------

    /**
     *  Adds a message to the buffer.
     *
     *  @param message message to be added.
     */
    public void receiveLogEvent(Object message) {
        if (message != null) {
            messageBuffer.add(message);
            LOGGER.debug("SimpleBuffer.receiveLogEvent, message = " + message);

            if (listeners.size() > 0) {
                processMessage(message);
            }
        }
    }


    /**
     *  {@inheritDoc}
     */
    public void addLogEventListener(LogEventListener listener, InputFilter filter) {
        throw new NotImplementedException("This implementation of LogBuffer disables using this method.");
    }


    /**
     *  {@inheritDoc}
     */
    public void addLogEventListener(LogEventListener listener) {
        if (listeners.contains(listener)) {
            //if listener is installed then do nothing
        } else {
            //otherwise add the listener to the list
            listeners.add(listener);
        }
    }


    /**
     *  Processes messages.
     *
     *  @param message message to be processed
     */
    private void processMessage(Object message) {
        for (LogEventListener ml : listeners) {
            ml.receiveLogEvent(message);
        }
    }
}
